package com.e3.basdoc.goodscls.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.goodscls.dao.GoodsClsDAO;
import com.e3.basdoc.goodscls.entity.GoodsClsEntity;
import com.e3.basdoc.goodscls.model.GoodsClsModel;
import com.e3.basdoc.goodscls.model.GoodsClsQC;
import com.e3.basdoc.goodscls.service.GoodsClsCacheManager;
import com.e3.basdoc.goodscls.service.GoodsClsQueryService;
import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SuperQueryService;

public class GoodsClsQueryServiceImpl extends SuperQueryService implements GoodsClsQueryService {
	
	private GoodsClsDAO goodsClsDAO;
	
	public GoodsClsDAO getGoodsClsDAO(){
		if(goodsClsDAO==null){
			goodsClsDAO = DAOHelper.createDAOInstance(GoodsClsDAO.class);
		}
		return goodsClsDAO;
	}
	
	public List<GoodsClsModel> getGoodsClsChildren(GoodsClsModel model)	throws ServiceException {
		if(model == null){
			return null;
		}
		long id = model.getCorpId();
		List<Long> pIds = new ArrayList<Long>(1);
		pIds.add(id);
		
		List<Long> ids = getGoodsClsDAO().getChildrenIds(pIds);
		return getGoodsClsByIds(ids);

	}
	
	public List<GoodsClsModel> getGoodsClsByIds(List<Long> ids) throws ServiceException {
		if(ids==null||ids.isEmpty()){
			return null;
		}
		List<GoodsClsModel> models = new ArrayList<GoodsClsModel>(ids.size());
		for (Long id : ids) {
			GoodsClsModel model = getGoodsClsById(id);
			models.add(model);
		}
		return models;
	}
	
	public GoodsClsModel getGoodsClsById(long id) throws ServiceException {
		GoodsClsModel model = GoodsClsCacheManager.INSTANCE.get(id);
		if(model!=null){
			return model;
		}
		
		GoodsClsEntity bankEntity = getGoodsClsDAO().getById(GoodsClsEntity.class, id);
		if(bankEntity==null){
			return null;
		}
		GoodsClsModel rModel = bankEntityToModel(bankEntity);
		long size = getGoodsClsDAO().getChildrenSize(id);
		if(rModel!=null){
			rModel.setHasChildren(size>0);
			GoodsClsCacheManager.INSTANCE.put(rModel);
		}
		return rModel;
	}
	
	public List<GoodsClsModel> getGoodsClsByCondition(GoodsClsQC qc) throws ServiceException{
		List<Long> ids = getGoodsClsDAO().getGoodsClsIdsByCondition(qc);
		if(ids!=null&&ids.size()>0){
			return getGoodsClsByIds(ids);
		}
		return null;
	}

	private GoodsClsModel bankEntityToModel(GoodsClsEntity entity) {
		
		if(entity == null){
			return null;
		}
		
		GoodsClsModel model = new GoodsClsModel();
		model = ocEntityToModel(model, entity);

		model.setId(entity.getId());
		model.setParentId(entity.getParentId());
		model.setCode(entity.getCode());
		model.setName(entity.getName());
		model.setAverageCost(entity.getAverageCost());
		model.setAverageLeadtime(entity.getAverageLeadtime());
		model.setAveragePrice(entity.getAveragePrice());
		model.setRemark(entity.getRemark());
				
		return model;

	}

	public List<GoodsClsModel> getAllGoodsClss() throws ServiceException {
		return getGoodsClsDAO().getAllGoodsClss();
	}
}
